 <template>
  <div class="goods-info">
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :banberlist="bannerlist" isfull="false"></swiper>  
        </div>
      </div>
    </div>

    <transition
      @before-enter="beforeEnter"
      @enter="enter"
      @after-enter="afterEnter">
      <div class="ball" v-show="ballflag" id="ball"></div>
    </transition>
    
    <div class="mui-card">
      <div class="mui-card-header">{{ currentGoods.title }}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：<del>￥{{ currentGoods.old }}</del>&emsp;&emsp;销售价：<span class="now-price">￥{{ currentGoods.now }}</span>
          </p>
          <p>购买数量：<number-box :max="currentGoods.count" @getcount="getGoodsCount"></number-box></p>
          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addShopCar">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品编号：{{ currentGoods.goodsnum }}</p>
          <p>库存情况：{{ currentGoods.count }}件</p>
          <p>上架时间：{{ currentGoods.addtime | dateFormat('YYYY-MM-DD') }}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="GoToImageInfo(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="GoToComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
import swiper from '../subcomponents/swiper.vue'
import { Toast } from 'mint-ui'
import numberbox from '../subcomponents/goodsinfo_numberbox.vue'

export default {
  data() {
    return {
      bannerlist: [],
      id: this.$route.params.id,
      currentGoods: {},
      ballflag: false,
      selectedCount : 1
    }
  },
  methods: {
    getGoodsList(goods_id) {
      this.$http.get('goods/'+goods_id).then(result => {
        this.currentGoods = result.body
        this.currentGoods.imgs.forEach((element, index) => {
          this.bannerlist.push({
            id: index+1,
            img: element
          })
        })
      }, error => {
        Toast('商品详情获取失败！')
      })
    },
    GoToComment(goods_id) {
      this.$router.push({ name: 'goodscomment', params: { id: goods_id } })
    },
    GoToImageInfo(goods_id) {
      this.$router.push({ name: 'goodsimageinfo', params: { id: goods_id } })
    },
    beforeEnter(el) {
      el.style.transform = 'translate(0, 0)'
    },
    enter(el, done) {
      el.offsetWidth
      // 根据不同的屏幕分辨率获取终点的位置
      var ball = document.getElementById('ball')
      var ShopCar = document.getElementById('ShopCar')

      var ballRect = ball.getBoundingClientRect()
      var shopCarRect = ShopCar.getBoundingClientRect()

      var offsetX = shopCarRect.left - ballRect.left
      var offsetY = shopCarRect.top - ballRect.top
      
      el.style.transform = `translate(${offsetX}px, ${offsetY}px)`
      el.style.transition = 'all 1s cubic-bezier(.63,-0.3,.67,.27)'
      done()
    },
    afterEnter(el) {
      this.ballflag= !this.ballflag
    },
    addShopCar() {
      this.ballflag = !this.ballflag

      // 构造一个商品信息对象
      var goods = {
        id: this.id,
        count : parseInt(this.selectedCount),
        price: this.currentGoods.now,
        title: this.currentGoods.title,
        selected: true
      }
      this.$store.commit('addToShopCar', goods)
    },
    getGoodsCount(count) {
      this.selectedCount = count
    }
  },
  components: {
    'swiper': swiper,
    'number-box': numberbox
  },
  created() {
    this.getGoodsList(this.id)
  }
};
</script>

<style lang="scss" scoped>
  .goods-info{
    background-color: #eee;
    overflow: hidden;
  }
  .mui-card-footer{
    display: block;
    button{
      margin: 15px 0;
    }
  }

  .ball{
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    z-index: 999;
    top: 390px;
    left: 146px;
  }
</style>
